package com.qiwx.pigeon.activity.home;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.adapter.home.RemindCheckEggAdapter;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.params.FertilizeParam;
import com.qiwx.pigeon.data.vo.CageBeanVo;
import com.qiwx.pigeon.data.vo.CageStatusRecordBean;
import com.qiwx.pigeon.data.vo.FertilizeBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.helper.DialogHelper;
import com.qiwx.pigeon.presenter.OperatePresenter;
import com.qiwx.pigeon.viewinterface.IOperateView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-06 17:14
 * 查蛋提醒事项
 **/
public class CheckEggActivity extends BaseRemindActivity implements IOperateView {

    List<CageBeanVo> data = new ArrayList<>();
    CageParam cageParam = new CageParam();
    int current_pos = -1;
    OperatePresenter operatePresenter;

//    @Override
//    public int getLayoutId() {
//        return R.layout.activity_check_egg;
//    }

    @Override
    public void initAfterOnCreate() {
        cageParam.setProduce_status(CageStatusConfig.PRODUCE_CD);
        cageParam.setPageSize(pageSize);
        initView();
        operatePresenter = new OperatePresenter(new WeakReference(this));
        // xRefreshView.startRefresh();
//        loadData(true);
    }

    @Override
    public void loadData(boolean isRefresh) {
        if (isRefresh) {
            pageNo = 1;
            data.clear();
        } else {
            pageNo++;
        }
        cageParam.setWh_id(wh_id);
        cageParam.setPageNum(pageNo);
        mPresenter.queryCageEggByPage(cageParam);
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        adapter = new RemindCheckEggAdapter(R.layout.adapter_remind_check_egg_list_item, data);
        return adapter;
    }

    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public int addListViewHead() {
        return R.layout.remind_check_egg_headview;
    }

    @Override
    public void initView() {
        super.initView();
        titleReturn.changeArrowLeft();
        titleContent.setText("待查蛋鸽笼详情列表");
        txtAmountName.setText("可查蛋鸽笼合计(窝)：");


        if (adapter != null && adapter instanceof RemindCheckEggAdapter) {
            ((RemindCheckEggAdapter) adapter).setIPigeonPairInterface((pos, c_id, methodId) -> {
                checkEgg(pos, c_id);
            });
        }
    }

    //查蛋
    public void checkEgg(int pos, int c_id) {
        current_pos = pos;
        DialogHelper.showCheckEggDialog(this, (param, methodId) -> {
            param.setWh_id(0);
            param.setNumber(0);
            param.setC_id(c_id);
            param.setAmount(data.get(pos).getAmount());
            param.setLay_egg_date(data.get(pos).getLay_egg_date());
            operatePresenter.updateCheckEggStatus(param);
        });
    }

    @Override
    public void onQueryCageSuccess(PageInfo<CageBeanVo> bean) {
        if (bean != null && bean.getList() != null) {
            data.addAll(bean.getList());
        }
        super.onQueryCageSuccess(bean);
    }

    @Override
    public void onUpdateCageStatusSuccess(int type) {
        if (current_pos >= 0) {
            data.remove(current_pos);
            adapter.notifyDataSetChanged();
            current_pos = -1;
        }
    }

    @Override
    public void onCheckEggSuccess(PageInfo<FertilizeBean> list) {

    }

    @Override
    public void onQueryCateStatusRecordSuccess(List<CageStatusRecordBean> list) {

    }

    @Override
    public void onSaveLayEggSuccess(int c_id) {

    }

    @Override
    public void onAddOperate(int type) {
        if (current_pos >= 0) {
            data.remove(current_pos);
            adapter.notifyDataSetChanged();
            current_pos = -1;
            updateAmount(-1);
        }

    }

    @Override
    public void onFailed(String error) {

    }
}
